<template>


  <div>

    <!--  查询-->
    <el-card>

      <el-form inline label-width="90px" size="mini" class="demo-form-inline">


        <el-form-item label="现场名称">
          <el-input placeholder="请输入现场名称" v-model.trim="searchOpsSiteParam.name" clearable ></el-input>
        </el-form-item>

        <el-form-item label="行政区划">
          <el-input placeholder="请输入所在行政区划代码" v-model.trim="searchOpsSiteParam.areaCode" clearable ></el-input>
        </el-form-item>

        <el-form-item label="总负责人">
          <el-input placeholder="请输入场所总负责人" v-model.trim="searchOpsSiteParam.leaderName" clearable></el-input>
        </el-form-item>

        <el-form-item label="负责人电话">
          <el-input placeholder="请输入负责人电话" v-model.trim="searchOpsSiteParam.leaderPhone" clearable ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="success" size="mini" @click="getPageListBefore">查询</el-button>
          <el-button type="info" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="warning" size="mini" @click="openSaveBefore">新增</el-button>
        </el-form-item>

      </el-form>

    </el-card>


    <!--列表-->
    <el-card style="margin-top: 10px; min-height: 700px">

      <div class="container">

        <!--一个小框框-->
        <div class="box" v-for="(item, index) in tableData" :key="index">
          <div class="box-content" >

            <el-row :gutter="10">
              <el-col :span="9">
                <div class="block">
                  <el-image class="siteIco" fit="contain" :src="item.icoUrl || require('@/assets/images/ico/site.png')" />
                </div>
              </el-col>
              <el-col :span="15">

                <el-row :gutter="10">
                  <el-col :span="16">
                    <h3 class="siteTitle" @click="openSiteLayout(item)">{{item.name}}</h3>
                  </el-col>
                  <el-col :span="8">
                    <el-tag size="mini" v-if="item.secrecy === 1" type="danger">保密单位</el-tag>
                    <el-tag size="mini" v-else-if="item.secrecy === 0"  >非保密单位</el-tag>
                  </el-col>
                </el-row>

                <el-row>
                  <el-col :span="8">负责人：</el-col>
                  <el-col :span="16">
                    <span>{{item.leaderName}}</span>
                  </el-col>
                </el-row>

                <el-row style="margin-top: 5px">
                  <el-col :span="8">阶段：</el-col>
                  <el-col :span="16">
                    <el-tag size="mini" type="info" v-if="item.stage === 1">启动阶段</el-tag>
                    <el-tag size="mini" type="danger" v-else-if="item.stage === 2">招投标阶段</el-tag>
                    <el-tag size="mini" type="warning" v-else-if="item.stage === 3">开发阶段</el-tag>
                    <el-tag size="mini" v-else-if="item.stage === 4">实施部署阶段</el-tag>
                    <el-tag size="mini" type="success" v-else-if="item.stage === 9">运维阶段</el-tag>
                  </el-col>
                </el-row>

                <el-row style="margin-top: 5px">
                  <el-col :span="8">重要性：</el-col>
                  <el-col :span="16">
                    <el-rate v-model="item.importance" disabled text-color="#ff9900"></el-rate>
                  </el-col>
                </el-row>

              </el-col>
            </el-row>

            <p style="margin-top: 0px; margin-bottom: 0px;">详情：</p>
            <p class="description" @click="openSiteLayout(item)">{{item.description}}</p>

            <!-- 固定在右下角的按钮组 -->
            <div class="button-group">
              <el-button type="primary" size="mini" @click="openUpdBefore(item)"  >编辑</el-button>
              <el-button type="danger" size="mini" @click="delTips(item)"  >删除</el-button>
            </div>


          </div>
        </div>
      </div>

      <!--分页部分-->
      <el-pagination
        style="width:33%; margin-right:10px; margin-top:10px;"
        background
        layout="total, prev, pager, next, jumper"
        @current-change="pageCurrentChange"
        :page-size="pageConfig.size"
        :current-page="pageConfig.current"
        :pager-count="5"
        :total="pageConfig.total">
      </el-pagination>

    </el-card>

    <OpsSiteSaveUpd ref="OpsSiteSaveUpdRef" @getOpsSitePageList="getOpsSitePageList"/>

  </div>

</template>



<script>

import OpsSiteSaveUpd from './OpsSiteSaveUpd'
import http from '@/utils/http'



export default {
  name: "OpsSiteList",
  components: {
    OpsSiteSaveUpd
  },
  data() {
    return {
      // 查询参数
      searchOpsSiteParam: {
        id: null,
        name: null,
        areaCode: null,
        address: null,
        leaderName: null,
        leaderPhone: null,
        logo: null,
        description: null,
        createTime: null,
        updateTime: null,
        updateId: null,
        createId: null,
        delType: null,
      },
      // 分页参数
      pageConfig: {
        // 每页记录数
        size: 20,
        // 当前页码
        current: 1,
        // 总记录数
        total: 0,
      },
      // 表格数据
      tableData: [],
    }
  },
  methods: {
    /**
     * 打开修改之前的的弹出框
     * @param obj 修改的值
     */
    openUpdBefore(obj) {
      this.$refs.OpsSiteSaveUpdRef.init('upd', obj)
    },
    /**
     * 删除之前的提示框
     * @param obj 要删除的数据
     */
    delTips(obj) {
      this.$confirm('是否执行删除, 删除后无法恢复，是否继续?', '提示', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消删除',
        type: 'warning'
      }).then(() => {
        // 确定删除
        this.delOpsSite(obj)

      }).catch(() => {
        // 取消删除
      });
    },
    /**
     * 删除
     * @param obj 要删除的数据
     */
    delOpsSite(obj) {
      http.post("/opsSite/delOpsSite", {
        siteId: obj.id,
      }).then(response => {
        let res = response.data;
        if (!res.success) {
          // 401是token过期跳转的
          if (res.code !== 401) {
            this.$message.error(res.msg);
          }
          return
        }

        this.getOpsSitePageList()
        this.$message.success("删除成功");

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 渲染图片
     * 只能这样做，图片这里不支持非固定地址返回
     * @param item 目标数据
     */
    getImageUrlInfo(item) {
      if (!item || !item.ico || item.ico === 'null') {
        // 判空或无效，直接显示默认图
        this.$set(item, 'icoUrl', null);
        return;
      }

      http.getImageUrl(item.ico)
        .then(url => {
          // 异步赋值，触发响应式更新
          this.$set(item, 'icoUrl', url);
        })
        .catch(() => {
          // 下载失败 fallback 默认图
          this.$set(item, 'icoUrl', null);
        });
    },
    /**
     * 打开场所详情
     * @param obj 这一行数据
     */
    openSiteLayout(obj){
      this.$router.push({path: '/toSiteLayout', query: {siteId: obj.id}})
    },
    /**
     * 打开保存的弹框
     */
    openSaveBefore() {
      this.$refs.OpsSiteSaveUpdRef.init('save', null)
    },
    /**
     * 分页被改变的时候
     * @param value 当前页
     */
    pageCurrentChange(value) {
      this.pageConfig.current = value
      this.getOpsSitePageList()
    },
    /**
     * 重置查询条件
     */
    resetQuery() {
      this.searchOpsSiteParam = {
        name: null,
        code: null,
      }
    },
    /**
     * 分页前重置分页参数
     */
    getPageListBefore() {
      this.pageConfig.current = 1
      this.getOpsSitePageList()
    },
    /**
     * 分页查询列表
     */
    getOpsSitePageList() {
      this.searchOpsSiteParam.current = this.pageConfig.current
      this.searchOpsSiteParam.size = this.pageConfig.size
      http.post("/opsSite/getSitePageList", this.searchOpsSiteParam).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        this.pageConfig.total = data.total;
        this.pageConfig.current = data.current;
        this.pageConfig.size = data.size;
        // 表格数据
        this.tableData = data.records
        // 异步加载每个图片
        this.tableData.forEach(item => this.getImageUrlInfo(item));

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
  },
  // 进入马上执行
  mounted() {
    this.getOpsSitePageList()
    this.$store.commit('navigationListChange', [
      {name: '现场管理', url: '/toSiteList', query: {}},
    ])
  },
}
</script>

<style scoped>

  .siteIco {
    width: 126px;
    height: 110px;
  }

  .container {
    display: flex;
    flex-wrap: wrap; /* 允许换行 */
    gap: 20px; /* 小框框之间的间距 */
    padding: 20px;
    min-height: 600px;
  }

  /*.box {
    width: 400px; !* 固定宽度 *!
    height: 250px; !* 固定高度 *!
    background-color: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
  }*/

  .box {
    width: 380px; /* 固定宽度 */
    height: 270px; /* 固定高度 */
    min-height: 270px;
    background-color: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
  }

  .box-content {
    flex: 1;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .box-content .description {
    margin-top: 0px;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3; /* 显示3行 */
    -webkit-box-orient: vertical;
    cursor: pointer;
  }

  .siteTitle {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    overflow: hidden;
    cursor: pointer;
  }

  /* 按钮组固定在右下角 */
  .button-group {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: auto; /* 关键属性，使按钮组固定在底部 */
    /*padding-top: 10px;*/
  }


</style>
